//
//  MineViewController.m
//  WuLi
//
//  Created by Gollum on 16/8/29.
//  Copyright © 2016年 Gollum. All rights reserved.
//

#import "MineViewController.h"
#import "MineTableViewCell.h"
#import "UserDetailViewController.h"
#import "WaveView.h"
#import "UIView+ZYDraggable.h"

#import "SetViewController.h"
#import "MissionCenterViewController.h"
#import "ExchangeMailViewController.h"
#import "MessageCenterViewController.h"
#import "MyCollectionViewController.h"
#import "IBetViewController.h"
#import "ActivityCenterViewController.h"
#import "YYAnimationView.h"
#import "UIImageView+WebCache.h"
#import "ShowLoginViewController.h"
#import "NetworkTool.h"
#pragma mark -  第三方
#import "Masonry.h"
#import "DKNightVersion.h"


#define kMineTableViewCellReuse @"MineReuse"
@interface MineViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic,strong)WaveView *waveView;
@property (nonatomic,strong)NSArray *titleArr;
@property (nonatomic,strong)NSArray *imageArr;
@property (nonatomic,strong)NSArray *contentArr;
@property (nonatomic,strong)UIImageView *imagView;

@property (weak, nonatomic) IBOutlet UIButton *userButton;

@property (weak, nonatomic) IBOutlet UILabel *userLabel;

@property (weak, nonatomic) IBOutlet UIButton *setButton;

@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (nonatomic,strong)NSUserDefaults *userDefaults;
@property (nonatomic,copy)NSString *imageUrl;
@property (nonatomic,copy)NSString *collectionCountStr;
@property (nonatomic,strong)NSMutableArray *collectionCount;

@end

@implementation MineViewController


- (void)viewWillAppear:(BOOL)animated{
    
    
    _userDefaults = [NSUserDefaults standardUserDefaults];
    _imageUrl = [_userDefaults objectForKey:@"userImageUrl"];
    if (_imageUrl) {
        NSURL *url = [NSURL URLWithString:_imageUrl];
        
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [[UIImage alloc] initWithData:data];
        [_userButton setImage:image forState:UIControlStateNormal];
        
        _userLabel.text = [_userDefaults objectForKey:@"userName"];
        [self handleData];
    }else{
    
        [_userButton setImage:[UIImage imageNamed:@"default_image"] forState:UIControlStateNormal];
        _userLabel.text = @"点击登录";
    }

    

}

- (void)viewDidLoad {
    [super viewDidLoad];
    //    self.tabBarController.tabBar.hidden = YES;
    
    _headerView.dk_backgroundColorPicker = DKColorWithColors([UIColor colorWithRed:0.99 green:0.81 blue:0.00 alpha:1.00], [UIColor colorWithRed:0.10 green:0.10 blue:0.10 alpha:1.00]);
    _userLabel.dk_textColorPicker = DKColorWithColors([UIColor blackColor], [UIColor colorWithRed:0.60 green:0.60 blue:0.59 alpha:1.00]);
   
//    self.view.dk_backgroundColorPicker = DKColorWithColors([UIColor whiteColor], [UIColor blackColor]);
//    
    
    _userButton.clipsToBounds = YES;
    _userButton.layer.cornerRadius = _userButton.frame.size.height / 2 + 5;
    [_userButton makeDraggable];
    
    
    
    
    _tableView.rowHeight = 60;
    _tableView.bounces = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    [_tableView registerNib:[UINib nibWithNibName:@"MineTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kMineTableViewCellReuse];
    _titleArr = @[@"任务中心", @"兑换商城", @"消息中心", @"我的收藏", @"我的押宝", @"活动中心", @"推荐给好友"];
    _imageArr = @[@"任务", @"兑换", @"消息", @"收藏", @"抵押", @"活动", @"推荐"];
    _contentArr = @[@" ", @" ", @" ", @" ", @" ", @" ", @" "];
    [_tableView reloadData];
}
#pragma mark -  该网络请求用来计算收藏的数量
- (void)handleData{
    
    NSString *urlString = @"http://211.151.51.100/v1/news/collect/list?";
    NSDictionary *header = @{@"User-Agent":@"Dalvik/2.1.0 (Linux; U; Android 5.0; ASUS_Z00ADB Build/LRX21V) Wuli/2.4.0 (agent:s;channel:pphl;credential:e1MyMDIwMDAxMDF9LXsjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIzM1Nzg3MjA2MTcxNDM4M30teyMjIyMjIyMxNDcyMDA5NzE2NTcwLTU0OTIyZjhiZTV9QDE0NzQwNzgyODczNjU=;deviceId:357872061714383;osTypeId:01;detailInfo:android;simTypeId:01;netTypeId:01;deviceTypeId:01;osVersion:5.0;idfa:null)", @"Host":@"api.5wuli.com"};
    [NetworkTool GET:urlString andBody:nil andHeader:header andResponse:JHJSON andSuccessBlock:^(id result) {
//        NSLog(@"_________________________我%@",result);
//        _collectionModel = [CollectionListModel modelWithDic:result];
//        [_dataArray addObject:_collectionModel];
//    _collectionCount = [result objectForKey:@"data"];
        NSMutableArray *array = [result objectForKey:@"data"];
        _collectionCountStr = [NSString stringWithFormat:@"%ld",array.count];
        
        [self.tableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    
    
    
}


- (void)viewDidLayoutSubviews {
    if (!self.waveView) {
        
        // Initialization
        self.waveView = [WaveView addToView:self.tableView.tableHeaderView withFrame:CGRectMake(0, CGRectGetHeight(self.tableView.tableHeaderView.frame) - 4.5, CGRectGetWidth(self.tableView.frame), 5)];
        
        // Optional Setting
        self.waveView.waveTime = 0.f;   // When 0, the wave will never stop;
        self.waveView.waveColor = [UIColor yellowColor];
        self.waveView.waveSpeed = 25.f;
        self.waveView.angularSpeed = 1.8f;
    }
 

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if ([self.waveView wave]) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.waveView stop];
            [self.tableView reloadData];
        });
    }
}

//返回分区数
//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//
//
//    return 1;
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (indexPath.row) {
        case 0:{
            MissionCenterViewController *mission = [[MissionCenterViewController alloc] init];
            [self presentViewController:mission animated:YES completion:^{
                
            }];
            
        }
            break;
        case 1:{
            ExchangeMailViewController *exchangeMall = [[ExchangeMailViewController alloc] init];
            [self presentViewController:exchangeMall animated:YES completion:^{
                
            }];
        }
            break;
        case 2:{
            MessageCenterViewController *messageCenter = [[MessageCenterViewController alloc] init];
            [self presentViewController:messageCenter animated:YES completion:^{
                
            }];
        }
            break;
        case 3:{
            MyCollectionViewController *myCollection = [[MyCollectionViewController alloc] init];
            [self presentViewController:myCollection animated:YES completion:^{
                
            }];
        }
            break;
        case 4:{
            IBetViewController *iBet = [[IBetViewController alloc] init];
            [self presentViewController:iBet animated:YES completion:^{
                
            }];
        }
            break;
        case 5:{
            ActivityCenterViewController *activityCenter = [[ActivityCenterViewController alloc] init];
            [self presentViewController:activityCenter animated:YES completion:^{
                
            }];
        }
            break;
        case 6:{
            
            [self showAnimationView];
            
            //            //添加手势---轻拍
            //            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
            //                [_imagView addGestureRecognizer:tap];
            //
        }
            break;
            
        default:
            break;
    }
    
}
- (void)showAnimationView
{
    NSArray *imgNameArray = @[@"朋友圈"
                              ,@"微信"
                              ,@"QQ"
                              ,@"QQ空间"
                              ,@"新浪微博"
                              ,@"复制链接"
                              ];
    
    NSArray *titleArray = @[@"朋友圈"
                            ,@"微信"
                            ,@"QQ"
                            ,@"QQ空间"
                            ,@"新浪微博"
                            ,@"复制链接"
                            ];
    YYAnimationView *animationView = [[YYAnimationView alloc] initImageNameArray:imgNameArray withItmeTitleArray:titleArray];
    animationView.lineItmeNumber = 3;
    animationView.itmeSize = CGSizeMake(100, 100);
    animationView.itmeTitleFont = [UIFont systemFontOfSize:15];
    [animationView addButtonItmeToAnimationView];
    [animationView showAnimationView];
    animationView.yyAnimationButtonItmeClick = ^( NSInteger tags){
        NSLog(@"%ld",(long)tags);
    };
}

//- (void)tapAction:(UITapGestureRecognizer *)tap{
//
//    [_imagView removeFromSuperview];
//
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 7;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kMineTableViewCellReuse];
    
    for (int i = 0; i < indexPath.row; i ++) {
        cell.picView.image = [UIImage imageNamed:_imageArr[indexPath.row]];
        cell.title.text = _titleArr[indexPath.row];
        cell.content.text = _contentArr[indexPath.row];
    }
    //判断收藏的个数
    if (indexPath.row == 3 && _imageUrl!= nil) {
        cell.content.text = _collectionCountStr;
    }else{
        cell.content.text = nil;
    
    }
    
    
    return cell;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

//头像点击方法
- (IBAction)userButton:(id)sender {
    
   
    if (_imageUrl) {
        UserDetailViewController *userDetail = [[UserDetailViewController alloc] init];
        [self presentViewController:userDetail animated:YES completion:^{
            
        }];
    }
    else{
        ShowLoginViewController *showLogin = [[ShowLoginViewController alloc] init];
        [self presentViewController:showLogin animated:YES completion:^{
            
        }];
    }
}
- (IBAction)setButton:(id)sender {
    
    SetViewController *seting = [[SetViewController alloc] init];
    [self presentViewController:seting animated:YES completion:^{
        
    }];
}


@end
